package com.dr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dr.common.ValueUtil;
import com.dr.pojo.Dept;
import com.dr.pojo.Invoice;
import com.dr.pojo.Regist;
import com.dr.pojo.Registlevel;
import com.dr.pojo.Scheduling;
import com.dr.pojo.User;
import com.dr.service.DeptService;
import com.dr.service.InvoiceService;
import com.dr.service.RegistService;
import com.dr.service.SchedulingService;
import com.dr.service.registlevelService;


@Controller
@RequestMapping("/rg")
public class RegistCtrl {
	
	@Autowired
	private registlevelService levelservice;
	@Autowired
	private RegistService registService;
	@Autowired
	private InvoiceService invoiceservice;
	@Autowired
	private SchedulingService schedulingService;
	@Autowired
	private DeptService deptservice;
	
	@RequestMapping("/regist")
	public String regist(Model md,Regist regist) {
		//发票号
		Invoice invoice =invoiceservice.nextInvoice(); 
		if(invoice !=null) {
			md.addAttribute("invoice", invoice);
		}else {
			md.addAttribute("invoice", new Invoice());
			md.addAttribute("errInvoice", "发票已用完，请重新申请");
		}
	
		//病历号
		String caseNumber =registService.nextCaseNumber();//病历号
		if(regist.getCaseNumber() == null) {
			md.addAttribute("caseNumber", caseNumber);//新的
		}else {
			md.addAttribute("caseNumber", regist.getCaseNumber());//旧的
		}
		
		//挂号级别
		List<Registlevel> levelList= levelservice.findRegistlevelList();
		if (levelList.size() != 0) {
			md.addAttribute("levelList", levelList);
		}else {
			md.addAttribute("levelList", new ArrayList<>());
			md.addAttribute("errRegistlevel", "获取挂号级别失败");
		}	
		List<Dept> deptList= deptservice.findDeptList();
		md.addAttribute("deptList", deptList);
		md.addAttribute("regist", regist);
		return "regist/regist.html";
	}
//	查询病历信息
	@RequestMapping("/caseInfo")
	public String caseInfo(Model md,String caseNumber) {
	//判断是否为空
		if(ValueUtil.isEmpty(caseNumber)) {
			caseNumber = "";
			}
		//根据病历号查询病历信息
			Regist regist = registService.findRegistByCaseNumber(caseNumber);
			if(regist == null) {
				regist =new Regist();
			}else {
				regist.setRegistLeID(0);  //查询病历不需要医生信息
				regist.setDeptName("");
				
			}
			md.addAttribute("regist", regist);
			System.out.println(regist);
			return regist(md, regist);
		
		}

	/*
	 * 查询排班医生
	 */
	@RequestMapping("/schedulingInfo")
	public String schedulingInfo(Model md,Regist regist) {
		List<Scheduling> schedulingList=schedulingService.findSchedulingList(regist);
		md.addAttribute("schedulingList",schedulingList);
		return regist(md,regist==null?new Regist():regist);
	}
//	/*
//	 * 执行挂号
//	 */
//		@ResponseBody
//		@RequestMapping(value="/doRegister")
//		public Map<String,Object> doRegister(Regist regist,HttpSession session){	
//			Map<String,Object> map = new HashMap<>();
//			//取出挂号员id
//			User user = (User) session.getAttribute("loginUser");
//			String time = regist.getBirthDate().toString();
//			//设置生日
//			if(ValueUtil.isNotEmpty(time)){			
//				java.sql.Date sDate = java.sql.Date.valueOf(time);
//				regist.setBirthDate(sDate);
//			}
//			//放入挂号员id
//			regist.setRegisterID(user.getId());
//			 System.out.println(regist);
//			 registService.insertRegist(regist);
//			map.put("status", "1");
//			return map;
//		}
	/*
	 * 执行挂号
	 */
//	@RequestMapping("/doRegist")
//	public String doRegist(Model md,Regist regist,HttpSession session) {
//		User loginUser=(User)session.getAttribute("loginUser");
//		regist.setRegisterID(loginUser.getId());
//		int count = registService.insertRegist(regist);
//		if(count==0) {
//			md.addAttribute("errMsg","挂号失败，请联系系统管理员");
//		}else {
//			invoiceservice.updateInvoice(regist.getId(),regist.getInvoiceNum());
//		}
//		return "/regist/fee.html";
//	}	

	
	}

